#ifndef SOS_SWAP_TABLE_H
#define SOS_SWAP_TABLE_H

/*
 * Suppose we have a maximum swap file of 2GiB.
 * This means tracking 524288 frames.
 *
 * Table design:
 *   - One word per frame. typedef'd to 'swap_table_entry'
 *   - The swap table takes the form of a linked list.
 *   - Each word contains either 0 or some swap number.
 *       0: The next free swap frame is the adjacent frame.
 *       N: The next free swap frame is N.
 *   - The first cell of the table points to the list head.
 *     This gives us constant-time operations, at the expense of a 2MiB table.
 *
 *   Operations:
 *     swap alloc: O(1). Take the list head from the first cell. Update the head
 *                      according to the contents of the free cell to be returned.
 *     Indexing: O(1).  Simple addition and shifting - it's an array.
 *     swap free: O(1). The returned frame is the new list head. The old list head
 *                      is its successor.
 */

#define SOS_SWAP_TABLE_SUCCESS   (0)
#define SOS_SWAP_TABLE_OOM       (1)
#define SOS_SWAP_TABLE_EXHAUSTED (2)

/* The first cell is used for the list head. This macro is for range checking */
#define SOS_SWAP_MAX (524287)

typedef seL4_Word swap_table_entry;
typedef swap_table_entry *swap_table;

/**
 * Initialise the swap table.
 */
int sos_swap_table_init(void);

/**
 * Obtain an unused swap number.
 */
int sos_swap_alloc(swap_table_entry *ste);

/**
 * Return a used swap number to the pool.
 */
int sos_swap_free(swap_table_entry ste);

#endif // SOS_SWAP_TABLE_H
